<template>
  <div class="app-container">
    <div class="menu-container">
      <el-form :inline="true" style="float: left;">
        <el-button size="mini" type="primary" @click="createGroup()">添加分组</el-button>
      </el-form>
      <div style="clear:both"/>
    </div>
    <el-table v-loading="listLoading" :data="page.records" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="名称">
        <template slot-scope="scope">
          <el-button type="text" size="small" icon="el-icon-edit" @click="modifyGroup(scope.row)">{{ scope.row.name }}</el-button>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="描述">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="排序">
        <template slot-scope="scope">
          <span>{{ scope.row.show_order }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="250">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="modifyGroupPermission(scope.row)">修改权限</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="delGroup(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="page.recordsTotal>0" :total="page.recordsTotal" :page.sync="listQuery.pageNum" :page-sizes="[20, 50, 100]" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog :visible.sync="dialogFormVisible" :title="dialogTitle" log- width="80%">
      <group-detail :is-edit="isEdit" :current-id="currentGroupId" @successGroup="successSaveGroup"/>
    </el-dialog>

    <el-dialog :visible.sync="dialogPermissionsFormVisible" title="分组权限管理" log- width="80%">
      <group-permission-detail :is-edit="true" :current-id="currentGroupId" @successGroupPermissions="successSaveGroupPermissions"/>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, deleteGroup } from '@/api/group'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import GroupDetail from './components/GroupDetail'
import GroupPermissionDetail from './components/GroupPermissionDetail'

export default {
  name: 'PermissionsList',
  components: { Pagination, GroupDetail, GroupPermissionDetail },
  data() {
    return {
      dialogTitle: '新增权限',
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20
      },
      isEdit: false,
      currentGroupId: undefined,
      formLabelWidth: '100px',
      dialogFormVisible: false,
      dialogPermissionsFormVisible: false,
      page: { pageNum: 1, pageSize: 20, pageTotal: 0, recordsTotal: 0, records: [] }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    modifyGroupPermission(group) {
      this.currentGroupId = group.id
      this.dialogPermissionsFormVisible = true
    },
    successSaveGroupPermissions() {
      this.dialogPermissionsFormVisible = false
      this.currentGroupId = undefined
    },
    successSaveGroup() {
      this.dialogFormVisible = false
      this.getList()
    },
    createGroup() {
      this.isEdit = false
      this.currentGroupId = undefined
      this.dialogTitle = '添加权限'
      this.dialogFormVisible = true
    },
    modifyGroup(group) {
      this.isEdit = true
      this.currentGroupId = group.id
      this.dialogTitle = '编辑权限'
      this.dialogFormVisible = true
    },
    delGroup(group) {
      deleteGroup(group.id).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
          return
        }
        this.$message({ 'message': '删除成功', 'type': 'success' })
        this.getList()
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.page = data.data
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
